<template>
	<view>
		<view class="bar" :style="{background: bg ? bg : `rgba(255,255,255, ${opacity})`}">
			<view class="box" :style="{top:top+'px',height:height+'px',padding: '0 ' + right+'px'}">
				<view class="icon" @click="goBack" >
					<u-icon name="arrow-left" :color="iconColor" size="22" v-if="isGoBack"></u-icon>
				</view>
				<view class="title" :style="{ color }">{{title || ''}}</view>
				<view class="rig"></view>
			</view>
			<u-gap :height="top + height + 10" v-if="is_gap"></u-gap>
		</view>
		<u-gap :height="top + height + 10" v-if="is_gap"></u-gap>
	</view>
	
</template>

<script>
	export default {
		name: "header",
		props: {
			title: {
				type: String,
				default: () => ''
			},
			is_gap: {
				type: Boolean,
				default: () => true
			},
			isGoBack: {
				type: Boolean,
				default: () => true
			},
			opacity: {
				type: Number,
				default: () => 1
			},
			lazy: {
				type: Boolean,
				default: () => false
			},
			color: {
				type: String,
				default: () => '#131313'
			},
			iconColor: {
				type: String,
				default: () => '#30323B'
			},
			bg: {
				type: String,
				default: () => ''
			}
		},
		data() {
			return {
				top: 0,
				height: 0,
				right: 15
			};
		},
		created() {
			// 获取胶囊按钮的位置信息
			let menuButtonRect = wx.getMenuButtonBoundingClientRect();
		

			this.top = menuButtonRect.top

			this.height = menuButtonRect.height


		},
		methods: {
			goBack() {
				if(this.lazy){
					this.$emit('goBackFn')
					return
				}
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		display: flex;
		position: fixed;
		z-index: 150;
		justify-content: space-between;
		box-sizing: border-box;
		width: 100vw;
		
		&>view {
			height: 100%;
			display: flex;
			align-items: center;
		}

		.title {
			font-family: PingFang SC;
			font-weight: 400;
			font-size: 30rpx;
			color: #131313;
		}
		.icon {
			width: 22rpx;
		}
		.rig {
			width: 22rpx;
		}
	}
	.bar {
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 150;
	}
</style>